/*******************************************************************************
  * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
  * @file     uiIcon.c
  * @author   UartTFT Application Team
  * @version  V0.0.1
  * @date     2023-01-04
  * @brief    This file contains the functions prototypes for the UartTFT
  *           Application display firmware.
 ********************************************************************************/
#include "uiIcon.h"
#include "uiEncoder.h"

#include "popupBox.h"
#include "levetop.h"
#include "transport.h"
#include "uart.h"
#include "function_drv.h"

/***********************************************************************************
* Function Name	: Display_Icon
* Description	: Show a icon.
* Input			: - id: icon id
				  - x,y: Screen coordinates
* Output        : None
* Return        : None
************************************************************************************/
void Display_Icon(uint16_t id, uint16_t x, uint16_t y, uint8_t Display_mode)
{
	uint8_t buf[12];
	_PICINFO icon;

	LT_ReadFlash_UI(buf, addr_index[10] + 12 * id + multi_language * 12 * addr_index[11], 12);
	icon.addr = (buf[0]) + (buf[1] << 8) + (buf[2] << 16) + (buf[3] << 24);
	icon.w = buf[4] + (buf[5] << 8);
	icon.h = buf[6] + (buf[7] << 8);
	icon.len = buf[8] + (buf[9] << 8) + (buf[10] << 16);
	icon.flag = buf[11];

	//  debug
//	 LTPrintf("Display_Icon id %d , xywh %d %d %d %d  addr %#x , flag %d  \r\n",id,x,y,icon.w,icon.h,icon.addr,icon.flag);

	if (icon.len != 0 && icon.flag != 0xFF)
	{
		if ((icon.flag >= ColorRGB565 && icon.flag <= ColorARGB4444_zip) || icon.flag == ColorARGB8565)
		{
			LT_ShowPicture(x, y, icon.w, icon.h, icon.addr, icon.len, icon.flag);
		}
	}
}

/***********************************************************************************
* Function Name : icon_display
* Description   : Show a icon.
* Input         : - type: icon info sources
				  - buff: icon info
* Output        : None
* Return        : None
************************************************************************************/
void icon_display(uint8_t type, uint8_t *buff)
{
	uint16_t p_add, p_len, pointer;
	uint16_t x, y, w, h,flag;
	uint16_t simage, eimage, nimage;
	uint16_t s_num, e_num;
	uint16_t d_num;
	uint8_t buff1[12] = {0}, i;
	uint8_t Display_Mode = 0;
	uint16_t overlay = 0;

	if (type == 0) // instruction information source is configuration file
	{
		Display_Mode = (buff[4] & 0x80) >> 7;
		p_add = (buff[5] + (buff[6] << 8));
		x = buff[7] + (buff[8] << 8);
		y = buff[9] + (buff[10] << 8);
		simage = buff[11] + (buff[12] << 8);
		eimage = buff[13] + (buff[14] << 8);
		s_num = buff[15] + (buff[16] << 8);
		e_num = buff[17] + (buff[18] << 8);
		overlay = buff[19] + (buff[20] << 8);
	}
	else if (type == 1) // instruction information ource is variable space
	{
		pointer = (buff[2] + (buff[3] << 8)) * 2;
		Display_Mode = (var[pointer]& 0x80) >> 7;
		p_add = (var[pointer + 1] + (var[pointer + 2] << 8));
		x = var[pointer + 3] + (var[pointer + 4] << 8);
		y = var[pointer + 5] + (var[pointer + 6] << 8);
		simage = var[pointer + 7] + (var[pointer + 8] << 8);
		eimage = var[pointer + 9] + (var[pointer + 10] << 8);
		s_num = var[pointer + 11] + (var[pointer + 12] << 8);
		e_num = var[pointer + 13] + (var[pointer + 14] << 8);
		overlay = var[pointer + 15] + (var[pointer + 16] << 8);
	}

	Var_AddrRemapping(&p_add);

	if (Pop_up_flag == 1) // Popup offset
	{
		x += Pop_up_x;
		y += Pop_up_y;
	}

	nimage = (var[p_add * 2] << 8) + var[p_add * 2 + 1];
	// LTPrintf("0000  Display_Icon  xy %d  %d  nimage,s_num,e_num, %d  %d %d  Display_Mode %d  \r\n",x,y,nimage,s_num,e_num,Display_Mode);
	
	if (nimage >= s_num && nimage <= e_num)
	{
		d_num = simage + nimage - s_num;
		if(overlay == 0xFFFF)
		{
			Display_Icon(d_num, x, y, Display_Mode);
		}
		else
		{
			LT_ReadFlash_UI(buff1, addr_index[10] + 12 * d_num + multi_language * 12 * addr_index[11], 12);
			pic_add = (buff1[0]) + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);
			w = buff1[4] + (buff1[5] << 8);
			h = buff1[6] + (buff1[7] << 8);
			pic_len = buff1[8] + (buff1[9] << 8) + (buff1[10] << 16);
			flag = buff1[11];

			if (flag == 1 || flag == 2)
			{
				LT268_BG_to_buffer(0, 0, w, m_PngData,x, y,w, h);
			}
			LT268_pic_to_buffer(0,0,w,m_PngData,
								0,0,w,pic_add,
								w,h,flag);

			Get_date_variable_Id_overlay(pic_id,overlay,x,y,w,h);

			LT268_buffer_to_TFT(0,0,w,m_PngData,
									x,y,w,h);
		}
	}
	else
	{
		LT_ReadFlash_UI(buff1, addr_index[10] + 12 * simage + multi_language * 12 * addr_index[11], 12);
		w = buff1[4] + (buff1[5] << 8);
		h = buff1[6] + (buff1[7] << 8);
		if(overlay == 0xFFFF)
		{
				LT268_dispaly_CutPicture(x, y,
											x, y, w, h,
											FullScreen_W, FullScreen_addr, FullScreen_color, FullScreen_flag);
		}
		else if (overlay >= 0x8000)
		{

			LT268_BG_to_buffer(0, 0, w, m_PngData,x, y,w, h);
			Get_date_variable_Id_overlay(pic_id,overlay,x,y,w,h);
			LT268_buffer_to_TFT(0,0,w,m_PngData,x,y,w,h);
		}
		else
		{
			LT268_BG_to_buffer(0, 0, w, m_PngData,x, y,w, h);
			Get_date_variable_Id_overlay(pic_id,overlay,x,y,w,h);
			LT268_buffer_to_TFT(0,0,w,m_PngData,x,y,w,h);
		}
	}
}

void icon_display_to_buff(uint8_t type, uint8_t *buff,uint16_t overlay_x, uint16_t overlay_y, uint16_t overlay_w,uint16_t overlay_h)
{
	uint16_t p_add, p_len, pointer;
	uint16_t x, y, w, h;
	uint16_t simage, eimage, nimage;
	uint16_t s_num, e_num;
	uint16_t d_num;
	uint8_t buf[12], i;
	uint8_t Display_Mode = 0;
	uint16_t overlay = 0;
	_PICINFO icon;

	if (type == 0) // instruction information source is configuration file
	{
		Display_Mode = (buff[4] & 0x80) >> 7;
		p_add = (buff[5] + (buff[6] << 8));
		x = buff[7] + (buff[8] << 8);
		y = buff[9] + (buff[10] << 8);
		simage = buff[11] + (buff[12] << 8);
		eimage = buff[13] + (buff[14] << 8);
		s_num = buff[15] + (buff[16] << 8);
		e_num = buff[17] + (buff[18] << 8);
		overlay = buff[19] + (buff[20] << 8);
	}
	else if (type == 1) // instruction information ource is variable space
	{
		pointer = (buff[2] + (buff[3] << 8)) * 2;
		Display_Mode = (var[pointer]& 0x80) >> 7;
		p_add = (var[pointer + 1] + (var[pointer + 2] << 8));
		x = var[pointer + 3] + (var[pointer + 4] << 8);
		y = var[pointer + 5] + (var[pointer + 6] << 8);
		simage = var[pointer + 7] + (var[pointer + 8] << 8);
		eimage = var[pointer + 9] + (var[pointer + 10] << 8);
		s_num = var[pointer + 11] + (var[pointer + 12] << 8);
		e_num = var[pointer + 13] + (var[pointer + 14] << 8);
		overlay = var[pointer + 15] + (var[pointer + 16] << 8);
	}

	Var_AddrRemapping(&p_add);

	if (Pop_up_flag == 1) // Popup offset
	{
		x += Pop_up_x;
		y += Pop_up_y;
	}

	nimage = (var[p_add * 2] << 8) + var[p_add * 2 + 1];
	if (nimage >= s_num && nimage <= e_num)
	{
		d_num = simage + nimage - s_num;

		LT_ReadFlash_UI(buf, addr_index[10] + 12 * d_num + multi_language * 12 * addr_index[11], 12);
		icon.addr = (buf[0]) + (buf[1] << 8) + (buf[2] << 16) + (buf[3] << 24);
		icon.w = buf[4] + (buf[5] << 8);
		icon.h = buf[6] + (buf[7] << 8);
		icon.len = buf[8] + (buf[9] << 8) + (buf[10] << 16);
		icon.flag = buf[11];

		LT268_pic_to_buffer(x - overlay_x,y - overlay_y,overlay_w,mix_ram,
							0,0,icon.w,icon.addr,
							icon.w,icon.h,icon.flag);
	}
}
